﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class problem
    {
        public static dynamic rejudgeProblemStatus(int pid)
        {
            var sql = new Sql.SqlDataContext();
            if (!sql.Problem.Any(row => row.pid == pid)) return new
            {
                success = false,
                msg = "题目不存在"
            };

            // Update Status
            var sts = sql.Status.Where(row => row.pid == pid);
            foreach (var s in sts)
            {
                if (s.status >= 3 && !s.god)
                {
                    // Update US
                    var us = sql.UserStatistics.FirstOrDefault(row => row.username == s.username);
                    if (us != null) user.deleteUserStatistics(us, s.status);
                }
                s.status = 0;
                s.exeTime = s.exeMem = s.reCode = null;
                s.ceInfo = s.exeOutput = s.specialOutput = null;
            }

            // Delete Solve
            sql.Solve.DeleteAllOnSubmit(sql.Solve.Where(row => row.pid == pid));

            // Reset PS
            var ps = sql.ProblemStatistics.FirstOrDefault(row => row.pid == pid);
            if (ps != null) ps.accept = ps.ce = ps.mle = ps.ole = ps.pe = ps.re = ps.se = ps.submit = ps.tle = ps.wa = 0;

            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
